﻿using System.Windows;
using System.Windows.Controls;

namespace Sidvall.UI.Wpf.Controls
{
    public partial class HeaderedXamDateTimeInput : UserControl
    {
        #region Public Members

        #region LabelMargin

        public static readonly System.Windows.DependencyProperty LabelMarginProperty = System.Windows.DependencyProperty.Register(nameof(LabelMargin),
            typeof(Thickness),
            typeof(HeaderedXamDateTimeInput),
            new System.Windows.FrameworkPropertyMetadata(new Thickness(0, 0, 0, 0), System.Windows.FrameworkPropertyMetadataOptions.None,
                null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public Thickness LabelMargin
        {
            get
            {
                return (Thickness)GetValue(LabelMarginProperty);
            }
            set
            {
                SetValue(LabelMarginProperty, value);
            }
        }

        #endregion
        #region LabelText

        public static readonly System.Windows.DependencyProperty LabelTextProperty = System.Windows.DependencyProperty.Register(nameof(LabelText),
            typeof(string),
            typeof(HeaderedXamDateTimeInput),
            new System.Windows.FrameworkPropertyMetadata(null, System.Windows.FrameworkPropertyMetadataOptions.None,
                OnLabelTextPropertyChanged, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public string LabelText
        {
            get
            {
                return GetValue(LabelTextProperty) as string;
            }
            set
            {
                SetValue(LabelTextProperty, value);
            }
        }

        #endregion
        #region LabelLeftContent

        public static readonly System.Windows.DependencyProperty LabelLeftContentProperty = System.Windows.DependencyProperty.Register(nameof(LabelLeftContent),
            typeof(object),
            typeof(HeaderedXamDateTimeInput),
            new System.Windows.FrameworkPropertyMetadata(null, System.Windows.FrameworkPropertyMetadataOptions.None,
                null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public object LabelLeftContent
        {
            get
            {
                return GetValue(LabelLeftContentProperty);
            }
            set
            {
                SetValue(LabelLeftContentProperty, value);
            }
        }

        #endregion
        #region LabelRightContent

        public static readonly System.Windows.DependencyProperty LabelRightContentProperty = System.Windows.DependencyProperty.Register(nameof(LabelRightContent),
            typeof(object),
            typeof(HeaderedXamDateTimeInput),
            new System.Windows.FrameworkPropertyMetadata(null, System.Windows.FrameworkPropertyMetadataOptions.None,
                null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public object LabelRightContent
        {
            get
            {
                return GetValue(LabelRightContentProperty);
            }
            set
            {
                SetValue(LabelRightContentProperty, value);
            }
        }

        #endregion
        #region DateTimeBackground

        public static readonly System.Windows.DependencyProperty DateTimeBackgroundProperty = System.Windows.DependencyProperty.Register(nameof(DateTimeBackground),
            typeof(System.Windows.Media.Brush),
            typeof(HeaderedXamDateTimeInput),
            new System.Windows.FrameworkPropertyMetadata(System.Windows.Media.Brushes.White, System.Windows.FrameworkPropertyMetadataOptions.None,
                null, null, false, System.Windows.Data.UpdateSourceTrigger.LostFocus));
        public System.Windows.Media.Brush DateTimeBackground
        {
            get
            {
                return GetValue(DateTimeBackgroundProperty) as System.Windows.Media.Brush;
            }
            set
            {
                SetValue(DateTimeBackgroundProperty, value);
            }
        }

        #endregion
        #region DateTimeBorderBrush

        public static readonly System.Windows.DependencyProperty DateTimeBorderBrushProperty = System.Windows.DependencyProperty.Register(nameof(DateTimeBorderBrush),
            typeof(System.Windows.Media.Brush),
            typeof(HeaderedXamDateTimeInput),
            new System.Windows.FrameworkPropertyMetadata(new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 171, 173, 179)),
                System.Windows.FrameworkPropertyMetadataOptions.None, null, null, false, System.Windows.Data.UpdateSourceTrigger.LostFocus));
        public System.Windows.Media.Brush DateTimeBorderBrush
        {
            get
            {
                return GetValue(DateTimeBorderBrushProperty) as System.Windows.Media.Brush;
            }
            set
            {
                SetValue(DateTimeBorderBrushProperty, value);
            }
        }

        #endregion
        #region DateTimeBorderThickness

        public static readonly System.Windows.DependencyProperty DateTimeBorderThicknessProperty = System.Windows.DependencyProperty.Register(nameof(DateTimeBorderThickness),
            typeof(Thickness),
            typeof(HeaderedXamDateTimeInput),
            new System.Windows.FrameworkPropertyMetadata(new Thickness(1), System.Windows.FrameworkPropertyMetadataOptions.None,
                null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public Thickness DateTimeBorderThickness
        {
            get
            {
                return (Thickness)GetValue(DateTimeBorderThicknessProperty);
            }
            set
            {
                SetValue(DateTimeBorderThicknessProperty, value);
            }
        }

        #endregion
        #region DateTimeDropDownButtonDisplayMode

        public static readonly System.Windows.DependencyProperty DateTimeDropDownButtonDisplayModeProperty = System.Windows.DependencyProperty.Register(nameof(DateTimeDropDownButtonDisplayMode),
            typeof(Infragistics.Controls.Editors.DropDownButtonDisplayMode),
            typeof(HeaderedXamDateTimeInput),
            new System.Windows.FrameworkPropertyMetadata(Infragistics.Controls.Editors.DropDownButtonDisplayMode.MouseOver, System.Windows.FrameworkPropertyMetadataOptions.None,
                null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public Infragistics.Controls.Editors.DropDownButtonDisplayMode DateTimeDropDownButtonDisplayMode
        {
            get
            {
                return (Infragistics.Controls.Editors.DropDownButtonDisplayMode)GetValue(DateTimeDropDownButtonDisplayModeProperty);
            }
            set
            {
                SetValue(DateTimeDropDownButtonDisplayModeProperty, value);
            }
        }

        #endregion
        #region DateTimeFontWeight

        public static readonly System.Windows.DependencyProperty DateTimeFontWeightProperty = System.Windows.DependencyProperty.Register(nameof(DateTimeFontWeight),
            typeof(System.Windows.FontWeight),
            typeof(HeaderedXamDateTimeInput),
            new System.Windows.FrameworkPropertyMetadata(System.Windows.FontWeights.Normal, System.Windows.FrameworkPropertyMetadataOptions.None,
                null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public System.Windows.FontWeight DateTimeFontWeight
        {
            get
            {
                return (System.Windows.FontWeight)GetValue(DateTimeFontWeightProperty);
            }
            set
            {
                SetValue(DateTimeFontWeightProperty, value);
            }
        }

        #endregion
        #region DateTimeHeight

        public static readonly System.Windows.DependencyProperty DateTimeHeightProperty = System.Windows.DependencyProperty.Register(nameof(DateTimeHeight),
            typeof(double),
            typeof(HeaderedXamDateTimeInput),
            new System.Windows.FrameworkPropertyMetadata(System.Double.NaN, System.Windows.FrameworkPropertyMetadataOptions.None, null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public double DateTimeHeight
        {
            get
            {
                return (double)GetValue(DateTimeHeightProperty);
            }
            set
            {
                SetValue(DateTimeHeightProperty, value);
            }
        }

        #endregion
        #region DateTimeHorizontalAlignment

        public static readonly System.Windows.DependencyProperty DateTimeHorizontalAlignmentProperty = System.Windows.DependencyProperty.Register(nameof(DateTimeHorizontalAlignment),
            typeof(HorizontalAlignment),
            typeof(HeaderedXamDateTimeInput),
            new System.Windows.FrameworkPropertyMetadata(HorizontalAlignment.Stretch, System.Windows.FrameworkPropertyMetadataOptions.None, null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public HorizontalAlignment DateTimeHorizontalAlignment
        {
            get
            {
                return (HorizontalAlignment)GetValue(DateTimeHorizontalAlignmentProperty);
            }
            set
            {
                SetValue(DateTimeHorizontalAlignmentProperty, value);
            }
        }

        #endregion
        #region DateTimeInvalidValueBehavior

        public static readonly System.Windows.DependencyProperty DateTimeInvalidValueBehaviorProperty = System.Windows.DependencyProperty.Register(nameof(DateTimeInvalidValueBehavior),
            typeof(Infragistics.Controls.Editors.InvalidValueBehavior),
            typeof(HeaderedXamDateTimeInput),
            new System.Windows.FrameworkPropertyMetadata(Infragistics.Controls.Editors.InvalidValueBehavior.Default, System.Windows.FrameworkPropertyMetadataOptions.None,
                null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public Infragistics.Controls.Editors.InvalidValueBehavior DateTimeInvalidValueBehavior
        {
            get
            {
                return (Infragistics.Controls.Editors.InvalidValueBehavior)GetValue(DateTimeInvalidValueBehaviorProperty);
            }
            set
            {
                SetValue(DateTimeInvalidValueBehaviorProperty, value);
            }
        }

        #endregion
        #region DateTimeIsEnabled

        public static readonly System.Windows.DependencyProperty DateTimeIsEnabledProperty = System.Windows.DependencyProperty.Register(nameof(DateTimeIsEnabled),
            typeof(bool),
            typeof(HeaderedXamDateTimeInput),
            new System.Windows.FrameworkPropertyMetadata(true, System.Windows.FrameworkPropertyMetadataOptions.None,
                null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public bool DateTimeIsEnabled
        {
            get
            {
                return (bool)GetValue(DateTimeIsEnabledProperty);
            }
            set
            {
                SetValue(DateTimeIsEnabledProperty, value);
            }
        }

        #endregion
        #region DateTimeIsReadOnly

        public static readonly System.Windows.DependencyProperty DateTimeIsReadOnlyProperty = System.Windows.DependencyProperty.Register(nameof(DateTimeIsReadOnly),
            typeof(bool),
            typeof(HeaderedXamDateTimeInput),
            new System.Windows.FrameworkPropertyMetadata(false, System.Windows.FrameworkPropertyMetadataOptions.None,
                null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public bool DateTimeIsReadOnly
        {
            get
            {
                return (bool)GetValue(DateTimeIsReadOnlyProperty);
            }
            set
            {
                SetValue(DateTimeIsReadOnlyProperty, value);
            }
        }

        #endregion
        #region DateTimeWidth

        public static readonly System.Windows.DependencyProperty DateTimeWidthProperty = System.Windows.DependencyProperty.Register(nameof(DateTimeWidth),
            typeof(double),
            typeof(HeaderedXamDateTimeInput),
            new System.Windows.FrameworkPropertyMetadata(System.Double.NaN, System.Windows.FrameworkPropertyMetadataOptions.None, null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public double DateTimeWidth
        {
            get
            {
                return (double)GetValue(DateTimeWidthProperty);
            }
            set
            {
                SetValue(DateTimeWidthProperty, value);
            }
        }

        #endregion
        #region Value

        public static readonly System.Windows.DependencyProperty ValueProperty = System.Windows.DependencyProperty.Register(nameof(Value),
            typeof(object),
            typeof(HeaderedXamDateTimeInput),
            new System.Windows.FrameworkPropertyMetadata(null, System.Windows.FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public object Value
        {
            get
            {
                return GetValue(ValueProperty);
            }
            set
            {
                SetValue(ValueProperty, value);
            }
        }

        #endregion

        #endregion
        #region Private Members

        #region InitializeLabel

        private static void InitializeLabel(HeaderedXamDateTimeInput parent)
        {
            if (string.IsNullOrWhiteSpace(parent.LabelText))
            {
                parent.Label.Visibility = Visibility.Collapsed;
            }
            else
            {
                parent.Label.Visibility = Visibility.Visible;
            }
        }

        #endregion

        #region OnLabelTextPropertyChanged

        private static void OnLabelTextPropertyChanged(System.Windows.DependencyObject d, System.Windows.DependencyPropertyChangedEventArgs e)
        {
            var parent = d as HeaderedXamDateTimeInput;
            if (parent == null)
                return;
            InitializeLabel(parent);
        }

        #endregion

        #endregion
        #region Constructors

        public HeaderedXamDateTimeInput()
        {
            InitializeComponent();
        }

        #endregion
    }
}
